package com.jlastu.znb.view;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jlastu.znb.R;
import com.jlastu.znb.contract.MineContract;
import com.jlastu.znb.utils.SpUtils;

public class MessageFragment extends Fragment {

    private MineContract.IMinePresenter mMinePresenter;
    private SpUtils mSpUtils;
    private int userId;
    public MessageFragment() {
        // Required empty public constructor
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 初始化SpUtils对象
        mSpUtils = new SpUtils(getContext());
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_message, container, false);
        LinearLayout buttonContainer = view.findViewById(R.id.xiaoxi);
        buttonContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击LinearLayout时的跳转逻辑
                jumpToTargetFragmentOrActivity();
            }
        });
        return view;
    }
    private void checkLoginStatusAndRedirect() {
        userId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1); // 只获取一次用户ID
        if (userId == -1) {
            redirectToLoginActivity();
            Toast.makeText(getContext(), "请登录后查看消息", Toast.LENGTH_SHORT).show();
        }
    }
    private void redirectToLoginActivity() {
        Intent intent = new Intent(getContext(), LoginActivity.class);
        startActivity(intent);
    }
    private void jumpToTargetFragmentOrActivity() {


         Intent intent = new Intent(getContext(), com.jlastu.znb.message.MainActivity.class);

         startActivity(intent);

    }
    @Override
    public void onResume() {
        super.onResume();
        checkLoginStatusAndRedirect(); // 调用检查登录状态的方法
        if (userId != -1 && mMinePresenter != null) {
            mMinePresenter.getLoginUser(userId); // 获取用户信息
        }
    }
}